package com.vivo.tasks.vivofx;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.modules.sys.entity.Area;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.service.AreaService;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.vivo.common.DatabaseContextHolder;
import com.vivo.fx.employee.entity.FxCompanyinfo;
import com.vivo.fx.employee.service.FxCompanyinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class TaskViOffice {

    @Autowired
    private OfficeService officeService;

    @Autowired
    private FxCompanyinfoService fxCompanyinfoService;

    @Autowired
    private AreaService areaService;

    public void SyncData() {
        // 获取安徽数据库数据
        getDataFromRemoteDB("ah");

        // 获取江苏数据库数据
        getDataFromRemoteDB("js");
    }

    private void getDataFromRemoteDB(String area) {
        boolean isAh = area.equals("ah");
        System.out.println("get office data from " + area + " db start...");

        Page<FxCompanyinfo> pageRes = null;

        try {
            // 切换数据库
            Helper.changeDB(area);

            int pageStart = 1;
            Page<FxCompanyinfo> page = new Page<FxCompanyinfo>(pageStart, 5000);
            page.setOrderBy("len(a.companycode)");

            pageRes = fxCompanyinfoService.findPage(page, new FxCompanyinfo());
        } finally {
            DatabaseContextHolder.setMainDb();
        }

        Area curArea = null;
        if (area.equals("ah")) {
            curArea = areaService.get("e90e62912ea749d7bad38abfcae20d54");
        } else {
            curArea = areaService.get("ad48fa845d31437799f33140e51cfee6");
        }

        for (FxCompanyinfo companInfo: pageRes.getList()) {
            // 查询该组织机构是否已经添加
            Office office = officeService.get(companInfo.getCompanyid());
            System.out.println("添加组织机构信息入库：" + companInfo.getCompanyname() + " with id: " + companInfo.getCompanyid());

            if (office == null) {
                office = new Office();

                if (companInfo.getParentcompanyid() == null || companInfo.getParentcompanyid().equals("")) {
                    office.setParent(null);
                    office.setParentIds("0,");
                }else {
                    Office parent = officeService.getVivoOffice(companInfo.getParentcompanyid());
                    if (parent != null) {
                        office.setParent(parent);
                        office.setParentIds(parent.getParentIds() + parent.getId() + ",");
                    } else {
                        office.setParent(null);
                        office.setParentIds("0,");
                    }
                }

                office.setIsTaskInsert(true);
                office.setId(companInfo.getCompanyid());
            }


            office.setName(companInfo.getCompanyname());
            office.setArea(curArea);
            office.setCode(companInfo.getCompanycode());
            office.setType(companInfo.getCompanytype());
            office.setGrade(companInfo.getCompanytype());
            office.setAddress(companInfo.getCompanyaddress());
            office.setMaster(companInfo.getPrincipalman());
            office.setPhone(companInfo.getContacttel());
            office.setEmail(companInfo.getEmail());
            office.setUseable("1");
            office.setIsOld("1");

            try {
                officeService.saveVivo(office);
            } catch (Exception e){
                System.out.println(e.getMessage());
                System.out.println("该组织已经同步过：" + companInfo.getCompanyname() + " with id: " + companInfo.getCompanyid());
            }
        }

        System.out.println("get data from " + area + " end.");
    }
}
